package com.xy.job.executor.log.factory

import com.xy.job.executor.log.logger.Log4j2Logger
import com.xy.job.executor.log.logger.Logger
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.spi.LoggerContext
import java.net.URL
import java.net.URLClassLoader

internal class Log4j2LoggerFactory : ILoggerFactory {

    private var loggerContext: LoggerContext

    init {
        Class.forName("org.apache.logging.log4j.core.Logger")
        val url: URL = Log4j2LoggerFactory::class.java.getResource("/log4j2-xy-job.xml")!!
        loggerContext = LogManager.getContext(
            URLClassLoader(emptyArray(), Thread.currentThread().contextClassLoader),
            false, null, url.toURI()
        )
    }

    override fun getLogger(name: String?): Logger {
        return Log4j2Logger(loggerContext.getLogger(name))
    }

}